package com.ruoyi.business.controller;

import com.ruoyi.business.domain.Emoticon;
import com.ruoyi.business.domain.bo.EmoticonBo;
import com.ruoyi.business.domain.vo.EmoticonVo;
import com.ruoyi.business.service.IEmoticonService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/18
 **/
@Api("表情包的控制器")
@RestController
@RequestMapping("/business/emoticon")
public class EmoticonController {

    @Autowired
    private ISysOssService sysOssService;

    @Autowired
    private IEmoticonService emoticonService;


    @PostMapping("/upload")
    public AjaxResult uploadEmo(@RequestParam("file") MultipartFile file) {
        if (file != null) {
            try {
                List<SysDictData> emoticonType = DictUtils.getDictCache("emoticon_type");
                List<EmoticonBo> list = FileUtils.readCsvToMap(file.getInputStream(), (strs) -> {
                    EmoticonBo bo = new EmoticonBo();
                    if (strs.length == 3) {
                        // 不是颜文字
                        for (String item : strs) {
                            if (item.startsWith("http")) {
                                bo.setBilibiliUrl(item);
                            } else if (item.startsWith("[") && item.endsWith("]")) {
                                bo.setName(item);
                            } else {
                                bo.setTypeName(item);
                            }
                        }
                    }else if (strs.length == 2) {
                        // 是颜文字
                        for (String item : strs) {
                            if ("颜文字".equals(item)) {
                                bo.setTypeName(item);
                            } else {
                                bo.setContext(item);
                            }
                        }
                    }

                    assert emoticonType != null;
                    for (SysDictData sysDictData : emoticonType) {
                        if (sysDictData.getDictLabel().equals(bo.getTypeName())) {
                            bo.setType(sysDictData.getDictValue());
                            break;
                        }
                    }
                    return bo;
                });
                emoticonService.insertEmoticon(list);
                return AjaxResult.success();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.error();
    }

    @GetMapping("/list")
    public AjaxResult selectList(EmoticonBo bo) {
        try {
            List<EmoticonVo> list = emoticonService.selectList(bo);
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    @GetMapping("/order")
    public AjaxResult order() {
        List<EmoticonVo> list = emoticonService.selectList(new EmoticonBo());
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        int index4 = 0;
        for (int i = 0; i < list.size(); i++) {
            EmoticonVo vo = list.get(i);
            Emoticon bean = new Emoticon();
            BeanUtils.copyProperties(vo, bean);
            if (vo.getType().equals("1")) {
                bean.setOrderNum(index1++);
            }else if (vo.getType().equals("2")) {
                bean.setOrderNum(index2++);
            }else if (vo.getType().equals("3")) {
                bean.setOrderNum(index3++);
            }else if (vo.getType().equals("4")) {
                bean.setOrderNum(index4++);
            }
            emoticonService.updateById(bean);

        }
        return null;
    }

}
